<?php
/*
用户操作类
@author jackbrown
@time 2012-1-06
@version 2.0.7
只适合单个用户的操作,相关表与表之间都是以user_id作为连接的主键，因此该字段是不能更改的，其它信息可以任意修改
主表user的字段是不能修改,但可以增加
用户属性
id           用户id
user_name    用户名
user_regip   用户注册ip
user_regtime 用户注册时间
user_pwd     用户注册密码
*/
class user extends infor{
	
	public $userName;
	
	private static $addItem = array();
	private static $mainItem = array();
	private static $loadItem = array();
	private static $adminItem = array();
	private static $groupItem = array();
	/*是否为管理员*/
	private $isAdmin = false;
	
	public function __construct($u,$admin=false){
		
		parent::__construct(PRE.'user','id');
		
		$this->isAdmin = $admin;
		if(is_numeric($u)){
			$this->userName = $this->getUserNameById($u);
		}else{
			$this->userName = $u;
		}
		
		if(self::$addItem == null){
			
			self::$addItem = $this->getTableFields(PRE.'user_addinfor');			
			self::$loadItem = $this->getTableFields(PRE.'onlineuser');
			self::$groupItem = $this->getTableFields(PRE.'usergroup');
		}
		
		if($admin==false){
			self::$mainItem = $this->getTableFields(PRE.'user');
		}else{
			self::$adminItem = $this->getTableFields(PRE.'admin_user');
			self::$mainItem = self::$adminItem;
		}
		
		
		$this->getUserInfor();
				
	}
	
	/**
	*用户注册
	*相关用户的规则判定，需要在类以外的过程中定义，以便于灵活使用
	*@param array $infor 信息数组;
	*@param true/false
	*/
	public function register($infor){
		
		if($infor==null || !is_array($infor)){
			return false;	
		}
		
		if($this->existsUser()){
			
			return false;	
		}
		
		
		$addItem = self::$addItem;	
		$mainInfor = array();
		
		/*用户主要信息*/
		if(!$this->isAdmin){
			
			$mainInfor['user_pwd'] = saltEncode($infor['user_pwd']);
			$mainInfor['user_name'] = $this->userName;
			$mainInfor['user_regip'] = getUserIp();
			$mainInfor['user_regtime'] = now();
			
			$this->insert(PRE.'user',$mainInfor);	
			
		}else{
			
			$mainInfor['user_pwd'] = saltEncode($infor['user_pwd']);
			$mainInfor['user_name'] = $this->userName;
			$mainInfor['user_group'] = $infor['user_group'];
			$this->insert(PRE.'admin_user',$mainInfor);	
		}
		
		
		/*读取相关信息到类*/
		$this->getUserInfor();
		$utype =  $this->isAdmin ? 2 : 1;
		
		$addInfor = array();
		$addInfor['user_id'] = $this->id;
		$addInfor['user_power'] = $utype;
		
		/**过滤**/
		if($infor != null){
						
			foreach($infor as $key=>$value){
				
				if(in_array($key,$addItem)){
					
					$addInfor[$key] = $value;	
				}
				
			}
	    }	
	    
		
		$res = true;
		if($addInfor!=null){
			$res = $res && $this->insert(PRE.'user_addinfor',$addInfor);
		}
		
		$res = $res && $this->insert(PRE.'onlineuser',array('user_state'=>'0','user_id'=>$this->id,'user_power'=>$utype));
		
		return $res;
	}
	
	
	/**
	*用户登录
	*@param pwd 用户的密码;
	*@return true/false;
	*/
	public function login($pwd){
		
		if($pwd == '' || !$this->existsUser()){
			
			return false;	
		}
		
		$pwd = saltEncode($pwd);
		$uPwd = $this->user_pwd;
		
		if($pwd!=$uPwd){
			
			return false;	
		}
		
		
		$hasLoad = $this->getUserInfor('user_hasload');
		if($hasLoad == null){
			$item = array();
			$item['user_id'] = $this->id;
			$item['user_power'] = $this->isAdmin ? 2 : 1;
			$loadFrm= new infor(PRE.'onlineuser','user_id');
			$loadFrm->add($item);
		}
		
		$loadTime = timeDistant($this->getUserInfor('user_lastreuquest'),$this->getUserInfor('load_time'),'s');
		
		return $this->setUserInfor(
			array(
				'user_ip'=>getUserIp(),
				'user_loadtime'=>now(),
				'user_state'=>'1',
				'user_lastrequest'=>now(),
				'user_hasload'=>($hasLoad+$loadTime),
			)
		);
		
		
	}
	
	/**
	*退出
	*/
	public function loginOut($session){
		
		if(!$this->existsUser()){
			return null;
		}
		
		if(!is_array($session)){
			session_unregister($session);
			unset($session);
		}else{
			
			foreach($session as $k=>$s){
				session_unregister($s);
			}
		}
		
		return $this->setUserInfor(
			array(
				'user_state'=>'0',
				'user_lastrequest'=>now(),
			)
		);
	}
	
	/**
	*是否存在此用户
	*/
	public function existsUser(){
		
		if($this->userName == ''){
			return false;	
		}
		
		if(!$this->isAdmin){
			
			return $this->recordCount("select id from ".PRE."user where user_name='".$this->userName."' limit 1");
			
		}else{
			return $this->recordCount("select id from ".PRE."admin_user where user_name='".$this->userName."' limit 1");
		}
	}
	
	/**
	*获取用户相关信息
	*@param string/array 用信息内容项，只能是数组或单个字段名
	*/
	public  function getUserInfor($item=''){
		
		if(!$this->existsUser()){
			return null;
		}

		if($item==null || $item == ''){
			
			if($this->id!=null){
				
				if(!$this->isAdmin){
					return array('id'=>$this->id,
								 'user_regip'=>$this->user_regip,
								 'user_regtime'=>$this->user_regtime
					);
				}else{
					
					return array('id'=>$this->id,
								 'user_group'=>$this->user_group,
					);
					
				}
			}
			
			if(!$this->isAdmin){
				$infor = $this->getRecords(PRE.'user','*','',"user_name = '".$this->userName."'",1);
			}else{
				$infor = $this->getRecords(PRE.'admin_user','*','',"user_name = '".$this->userName."'",1);	
			}
			
			
		}else{
			
			$utype 		=  $this->isAdmin ? 2 : 1;
			$mainTable  =  $utype == '1' ? PRE.'user' : PRE.'admin';
			
			if(is_array($item) && $item!=null){
				
				$loadFld = '';
				$addFld  = '';
				$groupFld = '';
				
				$mainFld = array();
				
				foreach($item as $key=>$pitem){
					
					if(in_array($pitem,self::$addItem)){
								
						$addFld .= "a.$pitem,";
						
					}
					
					if(in_array($pitem,self::$loadItem)){
						
						$loadFld .= "l.$pitem,";
					}
					
					if($this->isAdmin){
						
						if(in_array($pitem,self::$adminItem)){
							
							$mainFld[] = $pitem;
						}
						
					}else{
						if(in_array($pitem,self::$mainItem)){
							
							$mainFld[] = $pitem;
						}
					}
					
					if(in_array($pitem,self::$groupItem)){
						
						$groupFld.= "g.$pitem,";
					}
					
				}
				
			
				$addFld = stripLast($addFld,',');
				$loadFld = stripLast($loadFld,',');
				$groupFld = stripLast($groupFld,',');
				
				if($addFld=='' && $loadFld == '' && $mainFld == null && $groupFld == ''){
						
					$this->errorStr('要获取的用户信息类别不存在!');
					return false;
					
				}
				
				/*只有其它信息的情况*/
				if($addFld !='' && $loadFld == '' && $groupFld == ''){
					
					$infor = $this->getRecords(PRE.'user_addinfor as a',$addFld,'','a.user_id='.$this->id.' and a.user_power='.$utype,1);
				
				/*只有在线信息的情况*/
				}else if($addFld =='' && $loadFld != '' && $groupFld == ''){
					
					$infor = $this->getRecords(PRE.'onlineuser as l',$loadFld,'','l.user_id='.$this->id.' and l.user_power='.$utype,1);
				
				/*只有工作组的情况*/
				}else if($addFld =='' && $loadFld == '' && $groupFld != ''){
					
					$table = array('a'=>PRE.'admin_user','g'=>PRE.'usergroup');
					$fld = $groupFld;
					
					$infor = $this->getRecords(
									$table,
									$fld,'',
									"a.user_group = g.id and a.id='".$this->id."'",
									1
					);

				/**只有登录信息和附加信息的情况**/
				}else if($addFld !='' && $loadFld != '' && $groupFld == ''){							
				
					$table = array('a'=>PRE.'user_addinfor','l'=>PRE.'onlineuser');
					$fld = $addFld.','.$loadFld;
					
					$infor = $this->getRecords(
									$table,
									$fld,'',
									"a.user_id = '".$this->id."' and l.user_id='".$this->id."' and a.user_power=$utype and l.user_power=$utype",
									1
					);

				/**只有工作组和登录信息的情况**/
				}else if($addFld =='' && $loadFld != '' && $groupFld != ''){	
				
					$table = array('g'=>PRE.'usergroup','l'=>PRE.'onlineuser','u'=>PRE.'admin_user');
					$fld = $groupFld.','.$loadFld;
					
					$infor = $this->getRecords(
									$table,
									$fld,'',
									"u.user_group = g.id and u.id=l.user_id and l.user_id='".$this->id."' and l.user_power=".$utype,
									1
					);
				
				
				}else if($addFld !='' && $loadFld == '' && $groupFld != ''){	
				
					$table = array('g'=>PRE.'usergroup','a'=>PRE.'user_addinfor','u'=>PRE.'admin_user');
					$fld = $groupFld.','.$addFld;
					
					$infor = $this->getRecords(
									$table,
									$fld,'',
									"u.user_group = g.id and u.id=a.user_id and a.user_id='".$this->id."' and a.user_power=".$utype,
									1
					);
				
				}else if($addFld !='' && $loadFld != '' && $groupFld != ''){	
					
					$table = array('g'=>PRE.'usergroup','a'=>PRE.'user_addinfor','u'=>PRE.'admin_user','l'=>PRE.'onlineuser');
					$fld = $groupFld.','.$addFld.','.$loadFld;
					
					$infor = $this->getRecords(
									$table,
									$fld,'',
									"u.user_group = g.id and u.id=a.user_id  and l.user_id=u.id and l.user_id='".$this->id."' and a.user_power=$utype and l.user_power=$utype",
									1
					);
				
				
				}
				
				
				/*在装载类之时，主表信息已被装载，因此不需要再读。主表的相关信息*/
				if($mainFld != null){
					
					if(isset($mainFld['user_name'])){
						
						$mainFld['user_name'] = $this->userName;
					}
					
					foreach($mainFld as $key=>$value){
						$infor[0][$value] = $this->$value;
					}
					
					if(in_array('user_name',$mainFld)){
						
						$infor[0]['user_name'] = $this->userName;
					}
					
				}
				
			}else{
				
				if(strstr($item,',')){					
					$this->errorStr('用户项不能用,号分开,需由数组类型!');
					return false;
				}
					
					if(in_array($item,self::$addItem)){
								
						$infor = $this->getRecords(PRE.'user_addinfor',$item,'','user_id='.$this->id.' and user_power='.$utype,1);
						$this->$item = $infor[0][$item];
						
						return $infor[0][$item];
						
					}else if(in_array($item,self::$loadItem)){
						
						$infor = $this->getRecords(PRE.'onlineuser',$item,'','user_id='.$this->id.' and user_power='.$utype,1);
						$this->$item = $infor[0][$item];
						return $infor[0][$item];
					
					}else if(in_array($item,self::$mainItem)){
						return $this->$item;
									
					}else if(in_array($item,self::$groupItem)){
						
						$table = array('u'=>PRE.'admin_user','g'=>PRE.'usergroup');
						$item = $item == 'id' ? 'u.id' : $item;						
						$infor = $this->getRecords($table,$item,'','u.id='.$this->id.' and u.user_group = g.id ',1);
						$this->$item = $infor[0][$item];
						return $infor[0][$item];
					}
				
			}//eof 项是否为数组
			
			

		}	
		
		
		/*将项写入类的成员变量中，并返回一个数据或字符串*/
		$infor = $infor[0];	

		if($infor!=null){
			
			foreach($infor as $key=>$value){
				
				$this->$key = $value;
			}			
			
				
			return $infor;

		}		

	}
	
	/**
	*修改用户信息
	*主要是争对表addinfor,登录信息;用户主表信息，是不能修改,只读
	*@param array $item 用户修改信息数据 key=>value结构
	*/
	public function setUserInfor($item){
		
		if($item==null || !is_array($item) || !$this->existsUser()){
			
			
			return false;
		}
	
		$utype =  $this->isAdmin ? 2 : 1;
		$addFld = array();
		$loadFld = array();
		$admFld = array();
		$userFld = array();
		
		/*删除一些不可更改的项*/
		unset($item['id']);
		unset($item['user_name']);
		unset($item['user_regtime']);
		unset($item['user_pwd']);

		foreach($item as $fld=>$val){
				
			if(in_array($fld,self::$addItem)){
				
				$addFld[$fld] = $val;
				$this->$fld = $val;
			}
			
			if(in_array($fld,self::$loadItem)){
				
				$loadFld[$fld] = $val;
				$this->$fld = $val;
			}
			
			if($this->isAdmin){
				if(in_array($fld,self::$adminItem)){
					
						$this->$fld = $val;
						$admFld[$fld] = $val;
				}
			}
			
		}
		
		
		if($addFld==null && $loadFld == null && $admFld == null){
			
			$this->errorStr('要更新的项不存在，或为只读!');
			return false;
		}
		
		$isRight = true;
		if($addFld!=null){
			$isRight  = $isRight && $this->update(PRE.'user_addinfor',$addFld,"user_id=".$this->id.' and user_power='.$utype);
		}
		
		if($loadFld!=null){
			
			$isRight = $isRight && $this->update(PRE.'onlineuser',$loadFld,"user_id=".$this->id.' and user_power='.$utype);
		}
		
		if($this->isAdmin && $admFld!=null){
			
			$isRight = $isRight && $this->update(PRE.'admin_user',$admFld,"id=".$this->id);	
		}
		
		return $isRight;
	}
	
	/**
	*设置用户密码
	*@param string $newPwd新密码
	*@param string $oldPwd旧密码,如果不为空，则要求核对旧密码
	*@reuturn true/false;
	*/
	public function setPassword($newPwd,$oldPwd=''){
		
		if($newPwd ==''){
			
			return false;	
		}
		
		if($oldPwd != ''){
				
			$uPwd = $this->getUserInfor('user_pwd');
			
			if($uPwd != saltEncode($oldPwd)){
				
				return false;
			}
		}
		
		$newPwd = saltEncode($newPwd);
		
		if($this->isAdmin){
			return $this->update(PRE.'admin_user',array('user_pwd'=>$newPwd),'id='.$this->id);
		}else{
			return $this->update(PRE.'user',array('user_pwd'=>$newPwd),'id='.$this->id);
		}
	}
	
	/**
	*移出用户
	*/
	public function remove(){
		
		if($this->id){
			
			$s = true;
			if($this->isAdmin){
				
				$s = $s && $this->delById(PRE.'admin_user',$this->id);	
			}else{
				$s = $s && $this->delById(PRE.'user',$this->id);	
			}
			
			$utype =  $this->isAdmin ? 2 : 1;
			
			$s = $s && $this->deleteRecord(PRE.'onlineuser',"user_id=".$this->id." and user_power=".$utype);
			$s = $s && $this->deleteRecord(PRE.'user_addinfor',"user_id=".$this->id." and user_power=".$utype);
			
			return $s;
			
		}
		
		return false;
	}
		

	
	/**
	 * 通过id获取用户名
	 * @param int $id 用户的UID
	 */
	protected function getUserNameById($id){
	
		if($id){
			
			if($this->isAdmin){
				$u = $this->getRecords(PRE.'admin_user','user_name','','id='.$id,1);
			}else{
				$u = $this->getRecords(PRE.'user','user_name','','id='.$id,1);
			}

		}
		
		return $u[0]['user_name'];
	
	}
	
	private function errorStr($str){
		
		echo '<p><font color=red>'.$str.'</font>';
	}
	
	
}


?>